<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Option;
use app\admin\model\ClassSequence;

/**
 * 检查班级序号数据一致性的命令
 */
class CheckClassSequence extends Command
{
    protected function configure()
    {
        $this->setName('class_sequence:check')
            ->setDescription('Check and fix class sequence data consistency')
            ->addOption('fix', 'f', Option::VALUE_NONE, '自动修复发现的问题');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln("开始检查班级序号数据一致性...");
        
        try {
            // 检查数据一致性
            $inconsistencies = ClassSequence::checkConsistency();
            
            if (empty($inconsistencies)) {
                $output->writeln("<info>数据一致性检查通过，未发现问题！</info>");
                return 0;
            }
            
            $output->writeln("<warning>发现 " . count($inconsistencies) . " 个数据一致性问题：</warning>");
            
            $orphanedCount = 0;
            $typeMismatchCount = 0;
            $missingCount = 0;
            
            foreach ($inconsistencies as $item) {
                switch ($item['type']) {
                    case 'orphaned_sequence':
                        $orphanedCount++;
                        break;
                    case 'type_mismatch':
                        $typeMismatchCount++;
                        break;
                    case 'missing_sequence':
                        $missingCount++;
                        break;
                }
            }
            
            if ($orphanedCount > 0) {
                $output->writeln("- 孤立的序号记录: {$orphanedCount} 个");
            }
            if ($typeMismatchCount > 0) {
                $output->writeln("- 类型不匹配: {$typeMismatchCount} 个");
            }
            if ($missingCount > 0) {
                $output->writeln("- 缺失的班级序号: {$missingCount} 个");
            }
            
            // 如果指定了修复选项
            if ($input->getOption('fix')) {
                $output->writeln("开始修复数据一致性问题...");
                $result = ClassSequence::fixConsistency();
                
                $output->writeln("<info>修复完成！</info>");
                $output->writeln("- 删除孤立的序号记录: {$result['orphaned_removed']} 个");
                $output->writeln("- 修复类型不匹配: {$result['type_fixed']} 个");
                $output->writeln("- 添加缺失的班级序号: {$result['missing_added']} 个");
                
                // 再次检查一致性
                $inconsistencies = ClassSequence::checkConsistency();
                if (empty($inconsistencies)) {
                    $output->writeln("<info>所有问题已修复！</info>");
                } else {
                    $output->writeln("<warning>仍有 " . count($inconsistencies) . " 个问题未修复，可能需要手动处理。</warning>");
                }
            } else {
                $output->writeln("使用 --fix 选项可以自动修复这些问题。");
            }
            
            return 0;
        } catch (\Exception $e) {
            $output->writeln("<error>检查过程中发生错误: " . $e->getMessage() . "</error>");
            return 1;
        }
    }
}